package com.quectel.taskserver.mock.job;

import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.RandomUtil;
import com.quectel.business.common.alarm.DataProcessingComponent;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceColumnService;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.taskserver.mock.MockLimit;
import com.quectel.util.common.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 在这里编写说明
 *
 * @author: cl
 * @email: louis.cao@quectel.com
 * @date: 2022-04-15 09:57:00
 */
@Component
@Slf4j
public class AlarmMock implements SimpleJob {
    @DubboReference
    private VillageService villageService;
    @DubboReference
    private DeviceService deviceService;
    @DubboReference
    private DeviceColumnService deviceColumnService;
    @Autowired
    private DataProcessingComponent dataProcessingComponent;


    @Override
    public void execute(ShardingContext shardingContext) {
        if (!MockLimit.enableMock() || MockLimit.mockVillageCodes() == null) {
            return;
        }
        for (String villageCode : MockLimit.mockVillageCodes()) {
            VillageDto villageDto = villageService.selectByCode(villageCode);
            if (villageDto == null) {
                continue;
            }
            List<DeviceDto> result = new ArrayList<>();
            int pageNo = 0;
            Map<String, Object> queryDevicesMap = new HashMap<>();
            queryDevicesMap.put(LambdaUtil.getFieldName(DeviceDto::getVillageId), villageDto.getId());
            queryDevicesMap.put("limit", 1000);
            while (true) {
                queryDevicesMap.put("offset", pageNo++ * 1000);
                List<DeviceDto> list = deviceService.queryList(queryDevicesMap);
                if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)) {
                    break;
                }
                result.addAll(list);
                if (list.size() < 1000) {
                    break;
                }
            }
            List<DeviceDto> filteredList = RandomUtil.randomEleList(result, 100);

            log.info("start mock alarm data:{}", JacksonUtils.toJsonString(result.stream().map(DeviceDto::getUniqueId).collect(Collectors.toList())));

            for (DeviceDto deviceDto : filteredList) {

                HashMap<String, Object> params = new HashMap<>();
                params.put("status", new SecureRandom().nextInt(2) == 0 ? 0 : 1);
                dataProcessingComponent.handleData(deviceDto, JacksonUtils.toJsonString(params), params);
            }
        }

    }

}
